package com.ruoyi.qiuyue.controller.qiuyue;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.qiuyue.common.JWTUtils;
import com.ruoyi.qiuyue.common.UserConstat;
import com.ruoyi.qiuyue.domain.MusicUser;
import com.ruoyi.qiuyue.domain.vo.MusicUserVo;
import com.ruoyi.qiuyue.service.IMusicUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * 秋月用户控制类
 */
@RestController
@RequestMapping("/app/user")
public class QUserController {
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private IMusicUserService musicUserService;

    /**
     * 获取登录用户信息
     * @param request
     * @return
     */
    @GetMapping("/info")
    public AjaxResult userInfo(HttpServletRequest request) throws UnknownHostException {
        MusicUser user = jwtUtils.getDataByJwtToken(request);
        MusicUser musicUser = musicUserService.selectMusicUserById(user.getId());
        Map<String,String> data = new HashMap<>();
        data.put("img",musicUser.getHeadImg());
        data.put("name",musicUser.getUserName());
        return AjaxResult.success().put("data",data);
    }

    @PostMapping("/update")
    public AjaxResult updateInfo(HttpServletRequest request,@RequestBody MusicUserVo musicUserVo) {
        MusicUser user = jwtUtils.getDataByJwtToken(request);
        if (!StringUtils.isEmpty(musicUserVo.getUserName())){
            user.setUserName(musicUserVo.getUserName());
        }
        if(!StringUtils.isEmpty(musicUserVo.getPassword())){
            user.setPassword(musicUserVo.getPassword());
        }
        musicUserService.updateMusicUser(user);
        return AjaxResult.success(UserConstat.USER_UPDATE);
    }
}
